﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SaveGameTrigger : MonoBehaviour
{
    public string levelName = "寂静森林";
    public Vector3 offset = new Vector3(0,0.4f,0);
    bool hasSave=false;
    public void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag=="Player")
        {
            if (UIManager.Instance==null)
            {
                return;
            }
            UIManager.Instance.OpenSaveTip("E",transform,offset);
        }
    }
    public void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.tag == "Player")
        {
            if (UIManager.Instance == null)
            {
                return;
            }
            UIManager.Instance.CloseSaveTip();
            hasSave = false;
        }
    }
    public void OnTriggerStay2D(Collider2D collision)
    {
        if (collision.tag == "Player")
        {
            if (Input.GetKeyDown(KeyCode.E))
            {
                if (hasSave)
                    return;
                CharacterInfo characterInfo= collision.GetComponent<PlayerController2D>().characterInfo;
                SaveGame.SavePlayerInfo(characterInfo,collision.gameObject.transform,levelName);
                AudioManager.Instance.PlayAudio(Resources.Load<AudioClip>("Audio/ui_save"));
                hasSave = true;
            }
        }
    }

    public void OnDestroy()
    {
        if (UIManager.Instance == null)
        {
            return;
        }
        UIManager.Instance.CloseSaveTip();
    }


}
